package com.demo.controller;

import com.demo.model.Order;
import com.demo.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

@Tag(name = "订单管理", description = "订单相关接口")
@RestController
@RequestMapping("/api/orders")
@Validated
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Operation(summary = "获取所有订单", description = "返回所有订单列表", tags = {"订单管理"}, operationId = "getAllOrders")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Order.class))),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public ResponseEntity<List<Order>> getAllOrders() {
        return ResponseEntity.ok(orderService.getAllOrders());
    }

    @Operation(summary = "分页获取订单", description = "分页返回订单列表", tags = {"订单管理"}, operationId = "getOrdersByPage")
    @Parameters({
        @Parameter(name = "pageNum", description = "页码", required = true, example = "1"),
        @Parameter(name = "pageSize", description = "每页数量", required = true, example = "10")
    })
    @GetMapping("/page")
    public ResponseEntity<List<Order>> getOrdersByPage(@RequestParam int pageNum, @RequestParam int pageSize) {
        return ResponseEntity.ok(orderService.getOrdersByPage(pageNum, pageSize));
    }

    @Operation(summary = "根据ID获取订单", description = "通过订单ID获取订单详情", tags = {"订单管理"}, operationId = "getOrderById")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Order.class))),
        @ApiResponse(responseCode = "404", description = "未找到订单"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public ResponseEntity<Order> getOrderById(
            @Parameter(description = "订单ID", required = true, example = "1001") @PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        if (order == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(order);
    }

    @Operation(summary = "新增订单", description = "创建一个新订单", tags = {"订单管理"}, operationId = "addOrder")
    @ApiResponses({
        @ApiResponse(responseCode = "201", description = "创建成功"),
        @ApiResponse(responseCode = "400", description = "参数错误")
    })
    @PostMapping
    public ResponseEntity<Void> addOrder(
            @RequestBody(description = "订单信息", required = true, content = @Content(schema = @Schema(implementation = Order.class)))
            @org.springframework.web.bind.annotation.RequestBody @Validated Order order) {
        orderService.addOrder(order);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Operation(summary = "批量新增订单", description = "批量创建订单", tags = {"订单管理"}, operationId = "addOrders")
    @ApiResponses({
        @ApiResponse(responseCode = "201", description = "批量创建成功"),
        @ApiResponse(responseCode = "400", description = "参数错误")
    })
    @PostMapping("/batch")
    public ResponseEntity<Void> addOrders(
            @RequestBody(description = "订单列表", required = true, content = @Content(schema = @Schema(implementation = Order.class)))
            @org.springframework.web.bind.annotation.RequestBody List<Order> orders) {
        orderService.addOrders(orders);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Operation(summary = "上传订单文件", description = "通过文件上传订单数据", tags = {"订单管理"}, operationId = "uploadOrderFile")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "上传成功"),
        @ApiResponse(responseCode = "400", description = "文件格式错误")
    })
    @PostMapping("/upload")
    public ResponseEntity<String> uploadOrderFile(
            @Parameter(description = "订单文件", required = true) @RequestPart MultipartFile file) {
        // 这里只做演示
        return ResponseEntity.ok("文件上传成功: " + file.getOriginalFilename());
    }

    @Operation(summary = "更新订单", description = "根据订单ID更新订单信息", tags = {"订单管理"}, operationId = "updateOrder")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "更新成功"),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "404", description = "未找到订单")
    })
    @PutMapping
    public ResponseEntity<Void> updateOrder(
            @RequestBody(description = "订单信息", required = true, content = @Content(schema = @Schema(implementation = Order.class)))
            @org.springframework.web.bind.annotation.RequestBody @Validated Order order) {
        if (orderService.getOrderById(order.getId()) == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        orderService.updateOrder(order);
        return ResponseEntity.ok().build();
    }

    @Operation(summary = "删除订单", description = "根据订单ID删除订单", tags = {"订单管理"}, operationId = "deleteOrder")
    @Parameters({
        @Parameter(name = "X-Request-Source", description = "请求来源", in = ParameterIn.HEADER, required = false, example = "web")
    })
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "删除成功"),
        @ApiResponse(responseCode = "404", description = "未找到订单")
    })
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOrder(
            @Parameter(description = "订单ID", required = true, example = "1001") @PathVariable Long id,
            @RequestHeader(value = "X-Request-Source", required = false) String source) {
        if (orderService.getOrderById(id) == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        orderService.deleteOrder(id);
        return ResponseEntity.ok().build();
    }
} 